/**
 * 
 */
package org.springside.examples.miniweb.web.report.excel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import org.springside.examples.miniweb.entity.report.CompressorReport;

/**
 * @author Eric 2012-3-18
 * 
 * @since 6.0
 * 
 */
public class CompReportExcelView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String string = "压缩机维护列表"+ DateFormatUtils.format(new Date(),
						"yyyy-MM-dd")+".xls";
		response.setHeader("Content-Disposition", "attachment; filename="+ 
				new String(string.getBytes(), "iso8859-1")); 
		@SuppressWarnings("unchecked")
		List<CompressorReport> compList = (List<CompressorReport>) model.get("CompressorReport");
		compList = compList == null ? new ArrayList<CompressorReport>() : compList; 
		HSSFSheet sheet = workbook.createSheet("压缩机维护列表");
		HSSFRow header = sheet.createRow(0);
		String[] headers = new String[]{"维修ID","重点说明","客户名称","S/N","安排人员","客户订单号","维修单号","预计完成时间","入库","检测","试运行","检漏",
				"测试","维修","再测试","完修包装","装箱","出库","备注","更新时间"};
		for (int i = 0; i < headers.length; i++) {
			header.createCell(i).setCellValue(headers[i]);
		}

		int rowNum = 1;
		for (CompressorReport report : compList) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(report.getReportId());
			row.createCell(1).setCellValue(report.getKeyComment());
			row.createCell(2).setCellValue(report.getCustomerName());
			row.createCell(3).setCellValue(report.getSN());
			row.createCell(4).setCellValue(report.getAssignedUser());
			row.createCell(5).setCellValue(report.getCustomerOrderNumber());
			row.createCell(6).setCellValue(report.getMaintanceNumber());
			row.createCell(7).setCellValue(report.getScheduleFinish() == null ? "" : DateFormatUtils.format(report.getScheduleFinish(),
					"yyyy-MM-dd"));
			row.createCell(8).setCellValue(report.getInputRepository() == null ? "" : DateFormatUtils.format(report.getInputRepository(),
					"yyyy-MM-dd"));
			row.createCell(9).setCellValue( report.getValidation() == null ? "" :DateFormatUtils.format(report.getValidation(),
					"yyyy-MM-dd"));
			row.createCell(10).setCellValue( report.getTryRunning() == null ? "" :DateFormatUtils.format(report.getTryRunning(),
					"yyyy-MM-dd"));
			row.createCell(11).setCellValue( report.getLeak() == null ? "" :DateFormatUtils.format(report.getLeak(),
					"yyyy-MM-dd"));
			row.createCell(12).setCellValue( report.getTest() == null ? "" :DateFormatUtils.format(report.getTest(),
					"yyyy-MM-dd"));
			row.createCell(13).setCellValue(report.getMaintance() == null ? "" : DateFormatUtils.format(report.getMaintance(),
					"yyyy-MM-dd"));
			row.createCell(14).setCellValue( report.getMtest() == null ? "" :DateFormatUtils.format(report.getMtest(),
					"yyyy-MM-dd"));
			row.createCell(15).setCellValue( report.getFinishPackage() == null ? "" :DateFormatUtils.format(report.getFinishPackage(),
					"yyyy-MM-dd"));
			row.createCell(16).setCellValue(report.getPackages() == null ? "" : DateFormatUtils.format(report.getPackages(),
					"yyyy-MM-dd"));
			row.createCell(17).setCellValue(report.getOutputRepository() == null ? "" : DateFormatUtils.format(report.getOutputRepository(),
					"yyyy-MM-dd"));
			row.createCell(18).setCellValue(report.getComments());
			row.createCell(19).setCellValue(report.getUpdateTime() == null ? "" : DateFormatUtils.format(report.getUpdateTime(),
					"yyyy-MM-dd"));
		}
	}

}
